using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Management;
using System.Text;
using System.Net.Mail;

public partial class SiteAdmin : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Convert.ToBoolean(Session["sAdministrator"].ToString()) == false)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }

            DDLTimeZone1.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone1.DataValueField = "Id";
            DDLTimeZone1.DataTextField = "DisplayName";
            DDLTimeZone1.DataBind();

            DDLTimeZone2.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone2.DataValueField = "Id";
            DDLTimeZone2.DataTextField = "DisplayName";
            DDLTimeZone2.DataBind();

            DDLTimeZone3.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone3.DataValueField = "Id";
            DDLTimeZone3.DataTextField = "DisplayName";
            DDLTimeZone3.DataBind();

            DDLTimeZone4.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone4.DataValueField = "Id";
            DDLTimeZone4.DataTextField = "DisplayName";
            DDLTimeZone4.DataBind();

            BindData();        
        }        
    }
    protected void BindData()
    {
        string sqlQuery = "SELECT DBAContactID,DBAContactName,DBAOfficePhone,DBAMobilePhone,DBAHomePhone,DBANotificationFlag,DBAOnCallFlag FROM DBAContact";
        DataSet ds = db.ExecuteDataSet(CommandType.Text, sqlQuery);

        this.gv.DataSource = ds;
        this.gv.DataBind();

        String strAppName = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='AppName'").ToString();
        String strFooterText = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='FooterText'").ToString();

        this.txtAppName.Text = strAppName;
        this.txtFooterText.Text = strFooterText;

        String strTime1TimeZoneID = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1TimeZoneID'").ToString();
        String strTime2TimeZoneID = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2TimeZoneID'").ToString();
        String strTime3TimeZoneID = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3TimeZoneID'").ToString();
        String strTime4TimeZoneID = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4TimeZoneID'").ToString();

        this.DDLTimeZone1.Items.FindByValue(strTime1TimeZoneID.ToString()).Selected = true;
        this.DDLTimeZone2.Items.FindByValue(strTime2TimeZoneID.ToString()).Selected = true;
        this.DDLTimeZone3.Items.FindByValue(strTime3TimeZoneID.ToString()).Selected = true;
        this.DDLTimeZone4.Items.FindByValue(strTime4TimeZoneID.ToString()).Selected = true;

        String strTime1DisplayName = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1DisplayName'").ToString();
        String strTime2DisplayName = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2DisplayName'").ToString();
        String strTime3DisplayName = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3DisplayName'").ToString();
        String strTime4DisplayName = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4DisplayName'").ToString();

        this.txtTime1Display.Text = strTime1DisplayName;
        this.txtTime2Display.Text = strTime2DisplayName;
        this.txtTime3Display.Text = strTime3DisplayName;
        this.txtTime4Display.Text = strTime4DisplayName;

        this.imgLogo.ImageUrl = "~/Image/CompanyLogo.gif";

        string sqlQuery1 = "SELECT UserDomain,UserNetworkLogin,UserDomain +'\\'+UserNetworkLogin AS UserLogin,UserFullName,TimeZone,isAdmin FROM [User] ORDER BY UserFullName";
        DataSet ds1 = db.ExecuteDataSet(CommandType.Text, sqlQuery1);

        this.gv1.DataSource = ds1;
        this.gv1.DataBind();
   }
    protected void btnUpdateAppName_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtAppName.Text.ToString() + "' WHERE ConfigName = 'AppName'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sAppName"] = this.txtAppName.Text.ToString();
    }
    protected void btnUpdateFooterText_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtFooterText.Text.ToString() + "' WHERE ConfigName = 'FooterText'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sFooterText"] = this.txtFooterText.Text.ToString();
    }
    protected void btnUpdateTime2Display1_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtTime1Display.Text.ToString() + "' WHERE ConfigName = 'Time1DisplayName'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime1Display"] = this.txtTime1Display.Text.ToString();
    }
    protected void btnUpdateTime2Display_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtTime2Display.Text.ToString() + "' WHERE ConfigName = 'Time2DisplayName'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime2Display"] = this.txtTime2Display.Text.ToString();
    }
    protected void btnUpdateTime3Display_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtTime3Display.Text.ToString() + "' WHERE ConfigName = 'Time3DisplayName'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime3Display"] = this.txtTime3Display.Text.ToString();
    }
    protected void btnUpdateTime4Display_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.txtTime4Display.Text.ToString() + "' WHERE ConfigName = 'Time4DisplayName'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime4Display"] = this.txtTime4Display.Text.ToString();
    }
    protected void btnUpdateTime1TimeZone_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.DDLTimeZone1.SelectedItem.Value.ToString() + "' WHERE ConfigName = 'Time1TimeZoneID'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime1Time"] = this.DDLTimeZone1.SelectedItem.Value.ToString();
    }
    protected void btnUpdateTime2TimeZone_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.DDLTimeZone2.SelectedItem.Value.ToString() + "' WHERE ConfigName = 'Time2TimeZoneID'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime2Time"] = this.DDLTimeZone2.SelectedItem.Value.ToString();
    }
    protected void btnUpdateTime3TimeZone_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.DDLTimeZone3.SelectedItem.Value.ToString() + "' WHERE ConfigName = 'Time3TimeZoneID'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime3Time"] = this.DDLTimeZone3.SelectedItem.Value.ToString();
    }

    protected void btnUpdateTime4TimeZone_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE Config SET ConfigValue = '" + this.DDLTimeZone4.SelectedItem.Value.ToString() + "' WHERE ConfigName = 'Time4TimeZoneID'");
        db.ExecuteNonQuery(dbCommand);
        MessageBox.Show("Configuration Updated Successfully");
        BindData();

        Session["sTime4Time"] = this.DDLTimeZone4.SelectedItem.Value.ToString();
    }
    protected void btnChangeLogo_Click(object sender, EventArgs e)
    {
        this.imgLogo.Visible = false;
        this.btnChangeLogo.Visible = false;
        this.GetLogo.Visible = true;
        this.btnUpdateLogo.Visible = true;
    }
    protected void btnUpdateLogo_Click(object sender, EventArgs e)
    {
        if (this.GetLogo.HasFile)
        {
            try
            {
                GetLogo.SaveAs(Server.MapPath("~/Image/CompanyLogo.gif"));
                BindData();
                this.imgLogo.Visible = true;
                this.btnChangeLogo.Visible = true;
                this.GetLogo.Visible = false;
                this.btnUpdateLogo.Visible = false;
                this.lblStatus.ForeColor = System.Drawing.Color.Green;
                this.lblStatus.Text = "Company Logo successfully updated";
            }
            catch (Exception ex)
            {
                this.lblStatus.ForeColor = System.Drawing.Color.Red;
                this.lblStatus.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        
    }
}
